app.controller('AppCtrl',
    function($scope,$ionicLoading,$rootScope,$ionicHistory,appFactory,$ionicModal,
                                  $state,$cordovaBarcodeScanner,$cordovaInAppBrowser,Push,CheckVersion,
                                  $ionicPlatform,$cordovaGeolocation,$cordovaDevice,$User){
    var options = {
        location: 'no',
        clearcache: 'yes',
        toolbar: 'no'
    };

    var notificationCallback = function(data) {

        //var notification = angular.fromJson(data);
        //var platform = $cordovaDevice.getPlatform();
        //if('iOS' == platform){
        //    notification = notification.aps;
        //    Push.setBadge(0);
        //}
        //alert(notification.alert);
        $state.go('notifications');
    };



    $ionicPlatform.ready(function(){
        $ionicModal.fromTemplateUrl('templates/login.html', {
            scope: $scope
        }).then(function(modal) {
            $scope.modal = modal;
            if(!appFactory.isLogin()){
                modal.show();
            }else{
                Push.setAlias($User.get('id'));
                Push.init(notificationCallback);

            }
        });
    });

    $scope.loginData = {username:"",password:"",namenull:true,pwdnull:true};
        /**
         * 登录
         */

        $scope.doLogin = function(){
            $ionicLoading.show({});
            appFactory.doLogin($scope.loginData.username, $scope.loginData.password).then(function (res) {
                $ionicLoading.hide();
                switch (parseInt(res.code)){
                    case 0:
                        $scope.modal.hide();
                        window.location.href = "index.html";
                        break;
                    case -1:
                        alert("账号或密码错误");
                        break;
                    default :
                        alert("系统错误");
                        break;
                }
            }).catch(function(err){
                $ionicLoading.hide();
                switch (parseInt(err.code)){
                    case 0:
                        $scope.modal.hide();
                        window.location.reload();
                        break;
                    case -1:
                        alert("账号或密码错误");
                        break;
                    default :
                        alert("系统错误");
                        break;
                }
            });
        };
    $scope.showRanking = function() {
        //跳转至系统的浏览器中打开
        var link = 'http://61.147.75.91/apireport/reportJsp/showReport.jsp?raq=zwwlyhsz.raq';
        $cordovaInAppBrowser.open(link, '_system', options).then(function(event) {}).catch(function(event) {});

    };
    //用户收账明细
    $scope.showRanking1 = function() {
        //跳转至系统的浏览器中打开
        var link = 'http://61.147.75.91/apireport/reportJsp/showReport.jsp?raq=/zwwlszqd.raq';
        $cordovaInAppBrowser.open(link, '_system', options).then(function(event) {}).catch(function(event) {});

    };
    //分区收账明细
    $scope.showRanking2 = function() {
        //跳转至系统的浏览器中打开
        var link = 'http://61.147.75.91/demo/reportJsp/showReport.jsp?raq=/wlfqsc.raq';
        $cordovaInAppBrowser.open(link, '_system', options).then(function(event) {}).catch(function(event) {});
    };

    $scope.checkForUpdates = function() {

        CheckVersion.checkUpdate().then(function(){
            alert('当前已是最新版本');
        }).catch(function(e){
            if(!confirm('有新版本更新,是否前往更新?')){
                return false;
            }
            var link = e.download;
            //跳转至系统的浏览器中打开
            $cordovaInAppBrowser.open(link, '_system', options).then(function(event) {}).catch(function(event) {});
        });
    };
});


app.controller('AccountCtrl',['$scope','$ionicActionSheet','$ionicHistory','$timeout','$state','appFactory','NotificationFactory','$User','$rootScope',
    function($scope,$ionicActionSheet,$ionicHistory,$timeout,$state,appFactory,NotificationFactory,$User,$rootScope) {
        $rootScope.$on('$stateChangeStart', function(event,toState,fromState){
            if(toState.name === 'tab.setting'){
                $scope.accountInit() ;
            }
        });
        /**
         * 用户信息初始化
         */
        $scope.accountInit=function(){
            $scope.user = $User.get();
            //获取未读通知的数量
            NotificationFactory.count($User.get('id')).then(function(c){
                $scope.unread = c>0;
            })
        }
        $scope.accountInit();

        /**
         * 用户注销
         */
        $scope.loginOut=function(){
            if(confirm('是否确认要退出?')){
                appFactory.loginOut();
                //$state.go('tab.take');
                window.location.reload();
            }
        }
    }]);

app.controller('NotificationCtrl',['$scope','NotificationFactory','$rootScope','$User',function($scope,NotificationFactory,$rootScope,$User){
    $rootScope.$on('$stateChangeStart', function(event,toState,fromState){
        if(toState.name === 'notifications'){
            $scope.refresh() ;
        }
    });
    $scope.list = [];
    $scope.refresh = function(){
        $scope.list = [];
        NotificationFactory.list($User.get('id')).then(function(list){
            $scope.list = list;
        }).catch(function(err){
            console.log(err);
        }).finally(function(){
            $scope.$broadcast('scroll.refreshComplete');
        });
    };

    $scope.refresh();
}])
    .controller('NotificationDetailCtrl',['$scope','NotificationFactory','$stateParams',function($scope,NotificationFactory,$stateParams){
        var id = $stateParams.id;

        NotificationFactory.read(id).then(function(data){
            $scope.notification = data;
            console.log(data);
        }).catch(function(err){
            console.log(err);
        });
    }]);
app.controller('EcologicalCircleCtrl',['$scope','$cordovaInAppBrowser',"$User",function($scope,$cordovaInAppBrowser,$User){
    var uid = $User.get('id')
    var options = {
        location:'no',
        clearcache:'yes',
        closebuttoncaption:'关闭',
        toolbar:'yes'
    };
    $scope.openSells = function(){
        $cordovaInAppBrowser.open('http://logistics.guoran100.com/'+uid,'_blank',options).then(function(event){

        }).catch(function(err){
            alert(JSON.stringify(err));
        })
    };
    $scope.showRanking = function() {
        //跳转至系统的浏览器中打开
        var link = 'http://61.147.75.91/apireport/reportJsp/showReport.jsp?raq=zwwlyhsz.raq';
        $cordovaInAppBrowser.open(link, '_system', options).then(function(event) {}).catch(function(event) {});

    };
    //用户收账明细
    $scope.showRanking1 = function() {
        //跳转至系统的浏览器中打开
        var link = 'http://61.147.75.91/apireport/reportJsp/showReport.jsp?raq=/zwwlszqd.raq';
        $cordovaInAppBrowser.open(link, '_system', options).then(function(event) {}).catch(function(event) {});

    };
    //分区收账明细
    $scope.showRanking2 = function() {
        //跳转至系统的浏览器中打开
        var link = 'http://61.147.75.91/demo/reportJsp/showReport.jsp?raq=/wlfqsc.raq';
        $cordovaInAppBrowser.open(link, '_system', options).then(function(event) {}).catch(function(event) {});
    };

}]);